{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load Package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: replacing module ALBreIF.\n",
      "WARNING: replacing module BPGF.\n"
     ]
    }
   ],
   "source": [
    "include(\"ALBreIF/ALBreIF.jl\")\n",
    "include(\"BPGF/BPGF.jl\")\n",
    "using .ALBreIF\n",
    "using .BPGF\n",
    "\n",
    "using SparseArrays"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0.0 0.015530074593501534 … 0.06739369075803987 0.0; 0.0 0.0 … 0.0 0.0; … ; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0], [0.03219140688049787 0.00966273737899451 … 0.05077281593717662 0.02308132917547091; 0.050937705643587156 0.012201639827782303 … 0.0035154415458989634 0.005514671326618549; … ; 0.0014561004006169037 0.04311069479279823 … 0.05965188005658619 0.010386372565499992; 0.017466658355233283 0.02252649779843885 … 0.03425387176041628 0.04122659413440883])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = rand(Float64, 200, 200)\n",
    "A = BPGF.normalize!(A)\n",
    "X, Y = BPGF.randinit(A, 10, 0.2, 1.0) # initialize the fatorization matrices"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Configure parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ρ₀ = 0.8 # step size parameter\n",
    "μ₀ = 0.001 # regularization cofficient\n",
    "μ = 0.0 # another regularization coefficient\n",
    "rtime = 300 # runtime\n",
    "version = false"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Main.ALBreIF.Result{Float64}([0.015247127293271663 0.0 … 0.07584062212886929 0.07410412735947015; 0.010215586543615032 0.015497218146539414 … 0.020578096312898097 0.04721439342251479; … ; 0.040029386853868566 0.037427862254259266 … 0.0 0.0; 0.0 0.062215417750933165 … 0.028406951836074047 0.03614176011655274], [0.030125619461537592 0.014895330112931164 … 0.03652052245034442 0.028581654259902944; 0.027974201417042623 0.029815827631293597 … 0.0 0.0570542289494577; … ; 0.028248685886770476 0.0050050897953933795 … 0.015750164355841593 0.0; 0.013347390907986652 0.008017862304091064 … 0.028706444858485846 0.014870900638501968], 201192, true, [0.0 0.9999992821769633; 0.046000003814697266 0.9911878574690075; … ; 0.0 0.0; 0.0 0.0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X₀ = copy(X);\n",
    "Y₀ = copy(Y);\n",
    "r₀ = BPGF.solve!(BPGF.BPG{Float64}(runtime=rtime,\n",
    "                verbose=version,\n",
    "                ρ=0.02,\n",
    "                μ₁=μ₀,\n",
    "                μ₂=μ), A, X₀, Y₀)\n",
    "\n",
    "X₁ = copy(X);\n",
    "Y₁ = copy(Y);\n",
    "r₁ = BPGF.solve!(BPGF.BBPG{Float64}(runtime=rtime,\n",
    "                verbose=version,\n",
    "                ρ=0.02,\n",
    "                μ₁=μ₀,\n",
    "                μ₂=μ), A, X₁, Y₁)\n",
    "\n",
    "X₂ = copy(X);\n",
    "Y₂ = copy(Y);\n",
    "r₂ = ALBreIF.solve!(ALBreIF.ALBreI{Float64}(runtime=rtime,\n",
    "                verbose=version,\n",
    "                ρ=0.1,\n",
    "                μ₁=μ₀,\n",
    "                μ₂=μ), A, X₂, Y₂)\n",
    "\n",
    "X₃ = copy(X);\n",
    "Y₃ = copy(Y);\n",
    "r₃ = ALBreIF.solve!(ALBreIF.ABLBreI{Float64}(runtime=rtime,\n",
    "                verbose=version,\n",
    "                ρ=0.1,\n",
    "                μ₁=μ₀,\n",
    "                μ₂=μ), A, X₃, Y₃)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Store experimental results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "201192-element Vector{Float64}:\n",
       " 0.9999992821769633\n",
       " 0.9911878574690075\n",
       " 0.9829343418364549\n",
       " 0.9747036327417404\n",
       " 0.964910375905419\n",
       " 0.9545161041193545\n",
       " 0.9443707923769152\n",
       " 0.9360639498177462\n",
       " 0.9271642927568264\n",
       " 0.9174957928154066\n",
       " ⋮\n",
       " 0.20826624953457445\n",
       " 0.20826624863606727\n",
       " 0.20826624771688773\n",
       " 0.20826624651966583\n",
       " 0.20826624302781985\n",
       " 0.2082662405488593\n",
       " 0.20826623910532138\n",
       " 0.20826623828250668\n",
       " 0.2082662356589063"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "stop₀ = r₀.niters\n",
    "pic₀ = r₀.objvalue\n",
    "rt₀ = pic₀[1:stop₀, 1]\n",
    "obj₀ = pic₀[1:stop₀, 2]\n",
    "\n",
    "stop₁ = r₁.niters\n",
    "pic₁ = r₁.objvalue\n",
    "rt₁ = pic₁[1:stop₁, 1]\n",
    "obj₁ = pic₁[1:stop₁, 2]\n",
    "\n",
    "stop₂ = r₂.niters\n",
    "pic₂ = r₂.objvalue\n",
    "rt₂ = pic₂[1:stop₂, 1]\n",
    "obj₂ = pic₂[1:stop₂, 2]\n",
    "\n",
    "stop₃ = r₃.niters\n",
    "pic₃ = r₃.objvalue\n",
    "rt₃ = pic₃[1:stop₃, 1]\n",
    "obj₃ = pic₃[1:stop₃, 2]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "UndefVarError",
     "evalue": "UndefVarError: rt₀ not defined",
     "output_type": "error",
     "traceback": [
      "UndefVarError: rt₀ not defined\n",
      "\n",
      "Stacktrace:\n",
      " [1] speed()\n",
      "   @ Main d:\\gitee\\BLBreIF\\Experiment_synthesis_data.ipynb:7\n",
      " [2] with_theme(f::typeof(speed), theme::Attributes; kwargs::Base.Pairs{Symbol, Attributes, Tuple{Symbol}, NamedTuple{(:theme,), Tuple{Attributes}}})\n",
      "   @ Makie C:\\Users\\Administrator\\.julia\\packages\\Makie\\xhxRd\\src\\theming.jl:183\n",
      " [3] top-level scope\n",
      "   @ d:\\gitee\\BLBreIF\\Experiment_synthesis_data.ipynb:27"
     ]
    }
   ],
   "source": [
    "using CairoMakie\n",
    "using LaTeXStrings\n",
    "using Colors\n",
    "using AlgebraOfGraphics\n",
    "CairoMakie.activate!()\n",
    "function speed()\n",
    "        lines(rt₀, obj₀; color=\"#389826\", linewidth=2, linestyle=:solid,\n",
    "                label=\"BPG\",\n",
    "                figure=(; figure_padding=50, resolution=(1200, 800), font=\"sans\",\n",
    "                        backgroundcolor=:white, fontsize=32),\n",
    "                axis=(; title = L\"$\\text{Synthesis Matrix-Regularization} (\\mu_1=10^{-3})$\",\n",
    "                        xlabel=\"Time(sec)\", ylabel=L\"\\Vert A-XY\\Vert_F\",\n",
    "                        yscale=log10,\n",
    "                        xgridstyle=:dash, ygridstyle=:dash,\n",
    "                        topspinevisible = false, rightspinevisible =false))\n",
    "        lines!(rt₁, obj₁; color=\"blue\", linewidth=2, linestyle=:solid,\n",
    "                label=\"BBPG\")\n",
    "        lines!(rt₂, obj₂; color=\"#FFC633\", linewidth=2, linestyle=:solid,\n",
    "                label=\"ALBreI\")\n",
    "        lines!(rt₃, obj₃; color=\"#CB3C33\", linewidth=2, linestyle=:solid,\n",
    "                label=\"ABLBreI\")\n",
    "        limits!(0, rtime, 10^(-0.8), 1)\n",
    "        axislegend(merge=true)\n",
    "        current_figure()\n",
    "end\n",
    "\n",
    "with_theme(speed, theme=theme_dark())\n",
    "speed()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Save Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CairoMakie.Screen{IMAGE}\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "save(\"plot/synthesis_data.png\", speed())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "find_closest (generic function with 1 method)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "function find_closest(value, arr)\n",
    "    min_diff = abs(arr[1] - value)\n",
    "    closest_index = 1\n",
    "    \n",
    "    for i in 2:length(arr)\n",
    "        diff = abs(arr[i] - value)\n",
    "        if diff < min_diff\n",
    "            min_diff = diff\n",
    "            closest_index = i\n",
    "        end\n",
    "    end\n",
    "    \n",
    "    return closest_index\n",
    "end\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.22428210252911152\n",
      "0.21617369123536534\n",
      "0.21073235802735543\n",
      "0.20826623828250668"
     ]
    }
   ],
   "source": [
    "print(obj₃[find_closest(15, rt₃)])\n",
    "print(\"\\n\")\n",
    "print(obj₃[find_closest(30, rt₃)])\n",
    "print(\"\\n\")\n",
    "print(obj₃[find_closest(60, rt₃)])\n",
    "print(\"\\n\")\n",
    "print(obj₃[find_closest(300, rt₃)])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.8.3",
   "language": "julia",
   "name": "julia-1.8"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.8.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
